<script lang="ts" setup>
import {ref, onMounted, onUnmounted} from "vue";

const connnectData = ref([
  {
    "id": 1,
    "serverIp": "117.72.152.163",
    "serverUsername": "root",
    "serverPassword": "9800520@Zl",
    "userId": "1",
    "port": 22,
    "textShow": "服务器1",
    "folder": "重庆"
  },
  {
    "id": 2,
    "serverIp": "172.25.133.14",
    "serverUsername": "zhanghua",
    "serverPassword": "9800",
    "userId": "1",
    "port": 22,
    "textShow": "服务器2",
    "folder": "重庆"
  },
  {
    "id": 3,
    "serverIp": "111.170.33.15",
    "serverUsername": "root",
    "serverPassword": "BXcrBGIzEWm9",
    "userId": "1",
    "port": 48536,
    "textShow": "111.170.33.15",
    "folder": "重庆"
  },
  {
    "id": 4,
    "serverIp": "117.72.152.16",
    "serverUsername": "root",
    "serverPassword": "9800520@Zl",
    "userId": "1",
    "port": 22,
    "textShow": "服务器1",
    "folder": "北京"
  },
  {
    "id": 5,
    "serverIp": "172.25.133.17",
    "serverUsername": "zhanghua",
    "serverPassword": "9800",
    "userId": "1",
    "port": 22,
    "textShow": "服务器2",
    "folder": "北京"
  },
  {
    "id": 6,
    "serverIp": "111.170.33.18",
    "serverUsername": "root",
    "serverPassword": "BXcrBGIzEWm9",
    "userId": "1",
    "port": 48536,
    "textShow": "111.170.33.18",
    "folder": "北京"
  }
  ,
  {
    "id": 7,
    "serverIp": "111.170.33.19",
    "serverUsername": "root",
    "serverPassword": "BXcrBGIzEWm9",
    "userId": "1",
    "port": 48536,
    "textShow": "111.170.33.19",
    "folder": "上海"
  }
])

// 右键菜单状态
const contextMenuVisible = ref(false);
const contextMenuPosition = ref({x: 0, y: 0});
const selectedServer = ref<any>(null);
const selectedServerId = ref<number | null>(null);

// 显示右键菜单
const showContextMenu = (event: MouseEvent, server: any) => {
  event.preventDefault();
  selectedServer.value = server;
  selectedServerId.value = server.id;
  contextMenuPosition.value = {x: event.clientX, y: event.clientY};
  contextMenuVisible.value = true;
};

// 点击选中服务器
const selectServer = (server: any) => {
  selectedServer.value = server;
  selectedServerId.value = server.id;
};

// 双击连接服务器
const doubleClickConnect = (server: any) => {
  selectedServer.value = server;
  handleConnect();
};

// 隐藏右键菜单
const hideContextMenu = () => {
  contextMenuVisible.value = false;
  selectedServer.value = null;
};

// 菜单项点击事件
const handleConnect = () => {
  console.log('连接服务器:', selectedServer.value.serverIp);
  hideContextMenu();
};

const handleRename = () => {
  console.log('重命名服务器:', selectedServer.value.serverIp);
  hideContextMenu();
};

const handleEdit = () => {
  console.log('编辑服务器:', selectedServer.value.serverIp);
  hideContextMenu();
};

const handleDelete = () => {
  if (selectedServer.value) {
    const index = connnectData.value.findIndex(item => item.id === selectedServer.value.id);
    if (index > -1) {
      connnectData.value.splice(index, 1);
    }
  }
  hideContextMenu();
};

// 点击其他区域隐藏右键菜单
onMounted(() => {
  document.addEventListener('click', hideContextMenu);
});

onUnmounted(() => {
  document.removeEventListener('click', hideContextMenu);
});
</script>

<template>
  <div class="tab2-default">
    <ul>
      <li
          v-for="item in connnectData"
          :class="{ 'selected': selectedServerId === item.id }"
          class="fixed-layout-item"
          @click="selectServer(item)"
          @contextmenu="showContextMenu($event, item)"
          @dblclick="doubleClickConnect(item)"
      >
        {{ item.textShow }}
      </li>
    </ul>
  </div>

  <!-- 右键菜单 -->
  <div
      v-if="contextMenuVisible"
      :style="{ left: contextMenuPosition.x + 'px', top: contextMenuPosition.y + 'px' }"
      class="context-menu"
      @click.stop
  >
    <div class="menu-item connect" @click="handleConnect">连接</div>
    <div class="menu-item rename" @click="handleRename">重命名</div>
    <div class="menu-item edit" @click="handleEdit">编辑</div>
    <div class="menu-item delete" @click="handleDelete">删除</div>
  </div>
</template>

<style scoped>
.tab1-default,
.tab2-default {
  padding: 10px 0;
  width: 200px;
  background-color: transparent;
}

.fixed-layout-item {
  padding: 10px;
  margin-bottom: 8px;
  background-color: #f1eded;
  border-radius: 4px;
  width: 180px;
  box-sizing: border-box;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.fixed-layout-item:hover {
  background-color: #e6e6e6;
  transform: translateX(2px);
}

/* 选中状态样式 */
.fixed-layout-item.selected {
  background-color: #bbdefb;
  border: 2px solid #2196f3;
  font-weight: bold;
  color: #1565c0;
}

.fixed-layout-item.selected:hover {
  background-color: #90caf9;
}

/* 右键菜单样式 */
.context-menu {
  position: fixed;
  background-color: white;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 4px 0;
  min-width: 120px;
  z-index: 1000;
  font-size: 14px;
}

.menu-item {
  padding: 8px 16px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-item:hover {
  background-color: #f5f5f5;
}

.menu-item.connect:hover {
  background-color: #e3f2fd;
  color: #1976d2;
}

.menu-item.rename:hover {
  background-color: #fff3e0;
  color: #f57c00;
}

.menu-item.edit:hover {
  background-color: #e8f5e9;
  color: #388e3c;
}

.menu-item.delete:hover {
  background-color: #ffebee;
  color: #d32f2f;
}

/* 添加菜单项图标样式 */
.menu-item.connect::before {
  content: '🔌';
}

.menu-item.rename::before {
  content: '✏️';
}

.menu-item.edit::before {
  content: '⚙️';
}

.menu-item.delete::before {
  content: '🗑️';
}

/* 添加分隔线 */
.menu-item:not(:last-child) {
  border-bottom: 1px solid #f0f0f0;
}

/* 防止文本选择 */
.context-menu {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}
</style>